package edu.ajou.scw;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;

import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;

import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;


public class PConverter {

	public void convert(String team_id, String file)
	{		
		int sep = file.lastIndexOf("/");
		String savePath = file.substring(0, sep+1);
		String fileName = file.substring(sep+1);
		
		Log.d("", team_id, "Team " + team_id + " uses " + fileName);
		
		String type = fileName.substring(fileName.lastIndexOf("."));

		ArrayList<String> files = null;
		
		if (type.equals(".ppt")) {
			files = convertPPT(savePath, fileName);
		} 
		else if (type.equals(".pptx")){
			files = convertPPTX(savePath, fileName);
		}
		else if (type.equals(".pdf"))
		{
			files = convertPDF(savePath, fileName);
		}
		else
		{
	
		}

		if (files!=null)
		{
			int cnt = files.size();
			
			complete(team_id, cnt);
		}
	}
	
	public ArrayList<String> convertPPT(String path, String fileName)
	{
		try{
			FileInputStream is = new FileInputStream(path+fileName);
			
			ArrayList<String> files = new ArrayList<String>();
			
			SlideShow ppt = new SlideShow(is);
			Dimension pgsize = ppt.getPageSize();
			Slide[] slide = ppt.getSlides();
			
			for (int i = 0; i < slide.length; i++) {
				BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
				Graphics2D graphics = img.createGraphics();

				// clear the drawing area
				graphics.setPaint(Color.white);
				graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

				// render
				slide[i].draw(graphics);

				// save the output
				String imgName = "slide-" + (i + 1) + ".png";
				
				String pageUrl = path + imgName;

				files.add(imgName);

				ImageMaker.imageToFile(img, pageUrl);
				Log.d(fileName + " " + (i+1) + " / " + slide.length);
			}
			is.close();
			return files;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	public ArrayList<String> convertPPTX(String path, String fileName)
	{
		try {
			FileInputStream is = new FileInputStream(path+fileName);
			
			ArrayList<String> files = new ArrayList<String>();
			
			XMLSlideShow pptx = new XMLSlideShow(is);
		
			XSLFSlide[] slide = pptx.getSlides();
			Dimension pgsize = pptx.getPageSize();
			
			for (int i = 0; i < slide.length; i++) {
				BufferedImage img = new BufferedImage(pgsize.width,
						pgsize.height, BufferedImage.TYPE_INT_RGB);
				Graphics2D graphics = img.createGraphics();

				// clear the drawing area
				graphics.setPaint(Color.white);
				graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width,
						pgsize.height));
				
				// render
				slide[i].draw(graphics);

				// save the output
				String imgName = "slide-" + (i + 1) + ".png";
				String pageUrl = path + imgName;

				files.add(imgName);

				ImageMaker.imageToFile(img, pageUrl);
				Log.d(fileName + " " + (i+1) + " / " + slide.length);
			}
			is.close();
			return files;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	public ArrayList<String> convertPDF(String path, String fileName)
	{
		try {
			ArrayList<String> files = new ArrayList<String>();
			
			RandomAccessFile raf = new RandomAccessFile(path + fileName, "r");

			FileChannel channel = raf.getChannel();
			ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());

			PDFFile pdfFile = new PDFFile(buf);
			
			int pageCnt = pdfFile.getNumPages();
			
			for (int i=0; i<pageCnt; i++)
			{
				PDFPage page = pdfFile.getPage(i+1, true);
				Image image = page.getImage((int)page.getBBox().getWidth(), (int)page.getBBox().getHeight(), page.getBBox(), null, true, true);
				int width = image.getWidth(null);
				int height = image.getHeight(null);
				BufferedImage img = new BufferedImage(width,
						height, BufferedImage.TYPE_INT_RGB);
				Graphics2D graphics = img.createGraphics();
				graphics.drawImage(image, 0, 0, null);
				
				String imgName = "slide-" + (i + 1) + ".png";
				String pageUrl = path + imgName;
				ImageMaker.imageToFile(img, pageUrl);

				files.add(imgName);
				Log.d(fileName + " " + (i+1) + " / " + pageCnt);
			}
			raf.close();
			
			return files;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	public void complete(String team_id, int cnt)
	{
		DatabaseConnector conn = new DatabaseConnector();
		
		Team t = conn.getTeamById(team_id);
		
		ArrayList<String> slides = new ArrayList<String>();
		
		for (int i=0; i<cnt; i++)
		{
			slides.add("slide-" + (i+1) + ".png");
		}
		
		t.setSlides(slides);
		
		t.updateDb();
		
		t.inviteAll();
		
		Log.d("", team_id, "Team " + team_id + "'s parsing is completed.");
	}
}
